package cn.pupil.design.service;

import cn.pupil.design.common.ActivityInfo;
import cn.pupil.design.common.constant.Status;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description: 活动服务
 * @Author: pupil
 * @Date: 2024/07/30 下午 9:17
 */
public class ActivityService {

    private static Map<String, Enum<Status>> statusMap = new ConcurrentHashMap<>();

    /**
     * 初始化 活动信息
     *
     * @param activityId 活动ID
     * @param status     状态
     */
    public static void init(String activityId, Enum<Status> status) {
        // 模拟查询活动信息
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.setActivityId(activityId);
        activityInfo.setActivityName("早起学习打卡领奖活动");
        activityInfo.setStatus(status);
        activityInfo.setBeginTime(new Date());
        activityInfo.setEndTime(new Date());
        statusMap.put(activityId, status);
    }

    /**
     * 查询活动信息
     *
     * @param activityId 活动ID
     * @return 查询结果
     */
    public static ActivityInfo queryActivityInfo(String activityId) {
        // 模拟查询活动信息
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.setActivityId(activityId);
        activityInfo.setActivityName("早起学习打卡领奖活动");
        activityInfo.setStatus(statusMap.get(activityId));
        activityInfo.setBeginTime(new Date());
        activityInfo.setEndTime(new Date());
        return activityInfo;
    }

    /**
     * 查询活动状态
     *
     * @param activityId 活动ID
     * @return 查询结果
     */
    public static Enum<Status> queryActivityStatus(String activityId) {
        return statusMap.get(activityId);
    }

    /**
     * 执⾏行行状态变更
     * @param activityId 活动ID
     * @param beforeStatus 变更更前状态
     * @param afterStatus  变更更后状态
     */
    public static synchronized void execStatus(String activityId,Enum<Status> beforeStatus, Enum<Status> afterStatus){
            if (!beforeStatus.equals(statusMap.get(activityId))) return;
            statusMap.put(activityId,afterStatus);
    }


}
